package com.task.mall.controller.client;

import com.google.gson.Gson;
import com.task.mall.bean.*;
import com.task.mall.service.UserService;
import com.task.mall.service.impl.UserServiceImpl;
import com.task.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/api/client/user/*")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    Gson gson = new Gson();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/client/user/", "");
        if ("login".equals(action)){
            login(request,response);
        }else if ("signup".equals(action)){
            signup(request,response);
        }else if("updatePwd".equals(action)){
            updatePwd(request,response);
        }else if ("logoutClient".equals(action)){
            logoutClient(request,response);
        }else if ("updateUserData".equals(action)){
            updateUserData(request,response);
        }
    }

    /**
     * 保存修改的用户个人信息到数据库中去
     * @param request
     * @param response
     */
    private void updateUserData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        int result = userService.updateUserData(user);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        } else {
            res.setCode(10000);
            res.setMessage("当前访问异常，稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 退出商城
     * @param request
     * @param response
     */
    private void logoutClient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        // 下面这句好像没有用到
        Login login = gson.fromJson(requestBody, Login.class);
        Result res = new Result();
        res.setCode(0);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 修改个人账户的密码
     * @param request
     * @param response
     */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Password password = gson.fromJson(requestBody, Password.class);
        Result res = new Result();
        int result = userService.changePwd(password);
        if (result == 200){
            res.setCode(0);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 注册该tmall商城用户，需要设置用户名、地址、登录密码等相关信息
     * @param request
     * @param response
     */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        int result = userService.signup(user);
        Result res = new Result();
        Login loginInfo = userService.getLoginInfo(user.getEmail());
        if (result != 0){
            res.setCode(0);
            Map<String, Object> map = new HashMap<>(3);
            map.put("token",loginInfo.getNickName());
            map.put("name",loginInfo.getNickName());
            map.put("code",0);
            res.setData(map);
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 商城用户登录逻辑
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        int result = userService.login(user);
        Result res = new Result();
        if (result == 200){
            request.getSession().setAttribute("email", user.getEmail());
            Login loginInfo = userService.getLoginInfo(user.getEmail());
            res.setCode(0);
            Map<String, Object> map = new HashMap<>(3);
            map.put("token",loginInfo.getNickName());
            map.put("name",loginInfo.getNickName());
            map.put("code",0);
            res.setData(map);
        }else if (result == 404){
            res.setCode(10000);
            res.setMessage("用户名或者密码错误");
        }else{
            res.setCode(10000);
            res.setMessage("当前访问异常，稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/client/user/", "");
        if ("data".equals(action)){
            data(request,response);
        }

    }

    /**
     * 获取用户的个人资料 -- 以便修改
     * @param request
     * @param response
     */
    private void data(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        User user = userService.data(token);
        Result res = new Result();
        res.setCode(0);
        res.setData(user);
        response.getWriter().println(gson.toJson(res));
    }


}
